package com.drinks.yiniang.ui.widget.nav;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Space;

import com.drinks.yiniang.R;
import com.drinks.yiniang.common.listener.OnHomeNavigationBarClickListener;
import com.hansen.library.utils.AppUtils;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * 首页navigation bar
 * Created by han on 2018/10/17 0017.
 */

public class NavigationHomeBarLayout extends ConstraintLayout implements View.OnClickListener {

    private Context mContent;

    private AppCompatTextView mAddressTextView;
    private AppCompatTextView mSearchTextView;
    private AppCompatTextView mMessageTextView;

    private Drawable mAddressDrawableBlack;
    private Drawable mAddressDrawableWhite;
    private int mSearchResId;
    private Drawable mMessageDrawableBlack;
    private Drawable mMessageDrawableWhite;

    private String mAddressName;
    private String mSearchText;
    private String mMessageTitle;

    private int mAddressTextSize;
    private int mAddressTextColor;

    private int mSearchTextSize;
    private int mSearchTextColor;

    private int mMessageTextSize;
    private int mMessageTextColor;

    private int mSearchBackgroundResId;

    private boolean mDrawableIsWhiteTheme;

    private OnHomeNavigationBarClickListener onNavgationBarClickListener;

    public NavigationHomeBarLayout(Context context) {
        this(context, null);
    }

    public NavigationHomeBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationHomeBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initTest();

        initData();
    }

    private void initView(Context content, AttributeSet attrs) {
        mContent = content;

        TypedArray typedArray = mContent.obtainStyledAttributes(attrs, R.styleable.NavigationBarLayoutStyleable);

        mAddressDrawableBlack = typedArray.getDrawable(R.styleable.NavigationBarLayoutStyleable_navBarMenuResId);
        mSearchResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarSearchResId, R.mipmap.icon_nav_search_small);
        mMessageDrawableBlack = typedArray.getDrawable(R.styleable.NavigationBarLayoutStyleable_navBarMessageResId);

        mSearchBackgroundResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarSearchBackground, R.drawable.shape_bkg_home_search);

        mAddressName = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarMenuText);
        mSearchText = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarTitleText);
        mMessageTitle = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarEditText);

        mAddressTextSize = typedArray.getDimensionPixelSize(R.styleable.NavigationBarLayoutStyleable_navBarMenuTextSize, 10);
        mSearchTextSize = typedArray.getDimensionPixelSize(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextSize, 14);
        mMessageTextSize = typedArray.getDimensionPixelSize(R.styleable.NavigationBarLayoutStyleable_navBarEditTextSize, 10);

        mAddressTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarMenuTextColor, ContextCompat.getColor(content, R.color.color_main_black));
        mSearchTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextColor, ContextCompat.getColor(content, R.color.color_b4b4b4));
        mMessageTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarEditTextColor, ContextCompat.getColor(content, R.color.color_main_black));

        mDrawableIsWhiteTheme = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarWhiteTheme, false);

        typedArray.recycle();
    }

    private void initData() {
        int dp46 = ScreenSizeUtils.dp2px(mContent, 46);
        int dp5 = ScreenSizeUtils.dp2px(mContent, 5);
        int searchHeight = mContent.getResources().getDimensionPixelOffset(R.dimen.height_home_search);
        //获取顶部navbar高度
        int navBarHeight = mContent.getResources().getDimensionPixelSize(R.dimen.height_navigation_bar);

        //沉浸，设置状态栏高度, 加上&& !isInEditMode() 判断是因为在EditMode模式下，会出现exception
        if (AppUtils.isVersionCodeGreaterThanOrEqualToKITKAT() && !isInEditMode()) {
            int statusBarHeight = (int) ScreenSizeUtils.getStatusBarHeightWithAndroid(mContent);
            setPadding(0, statusBarHeight, 0, 0);
        }

        mAddressTextView = new AppCompatTextView(mContent);
        mSearchTextView = new AppCompatTextView(mContent);
        mMessageTextView = new AppCompatTextView(mContent);

        //菜单
        mAddressTextView.setId(R.id.iv_nav_bar_menu);
        mAddressTextView.setPadding(0, 0, 0, 0);
        mAddressTextView.setOnClickListener(this);
        mAddressTextView.setGravity(Gravity.CENTER);
        mAddressTextView.setLines(1);
        mAddressTextView.setEllipsize(TextUtils.TruncateAt.END);
        mAddressTextView.setTextColor(mAddressTextColor);
        mAddressTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mAddressTextSize);
        mAddressTextView.setText(mAddressName);
        mAddressTextView.setCompoundDrawablePadding(3);

        //搜索
        mSearchTextView.setId(R.id.iv_nav_bar_search);
        mSearchTextView.setPadding(dp5 * 3, 0, dp5 * 3, 0);
        mSearchTextView.setOnClickListener(this);
        mSearchTextView.setGravity(Gravity.CENTER_VERTICAL);
        mSearchTextView.setLines(1);
        mSearchTextView.setEllipsize(TextUtils.TruncateAt.END);
        mSearchTextView.setTextColor(mSearchTextColor);
        mSearchTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mSearchTextSize);
        mSearchTextView.setText(mSearchText);
        mSearchTextView.setCompoundDrawablePadding(dp5);
        mSearchTextView.setCompoundDrawablesWithIntrinsicBounds(mSearchResId, 0, 0, 0);
        mSearchTextView.setBackgroundResource(mSearchBackgroundResId);

        //关注
        mMessageTextView.setId(R.id.iv_nav_bar_message);
        mMessageTextView.setPadding(0, 0, 0, 0);
        mMessageTextView.setOnClickListener(this);
        mMessageTextView.setGravity(Gravity.CENTER);
        mMessageTextView.setLines(1);
        mMessageTextView.setEllipsize(TextUtils.TruncateAt.END);
        mMessageTextView.setTextColor(mMessageTextColor);
        mMessageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mMessageTextSize);
        mMessageTextView.setText(mMessageTitle);
        mMessageTextView.setCompoundDrawablePadding(3);

        Space space = new Space(mContent);
        space.setId(R.id.iv_nav_bar_space);

        //设置drawable theme
        if (mDrawableIsWhiteTheme) {
            setNavBarDrawableWhite();
        } else {
            setNavBarDrawableBlack();
        }

        addView(space);
        addView(mAddressTextView);
        addView(mSearchTextView);
        addView(mMessageTextView);

        //添加依赖关系
        ConstraintSet set = new ConstraintSet();

        set.clone(this);

        //space
        set.constrainWidth(space.getId(), ConstraintSet.WRAP_CONTENT);
        set.constrainHeight(space.getId(), navBarHeight);

        //address
        set.constrainWidth(mAddressTextView.getId(), dp46);
        set.constrainHeight(mAddressTextView.getId(), ConstraintSet.WRAP_CONTENT);
        set.connect(mAddressTextView.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT);
        set.connect(mAddressTextView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mAddressTextView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);

        //MessageTextView
        set.constrainWidth(mMessageTextView.getId(), dp46);
        set.constrainHeight(mMessageTextView.getId(), ConstraintSet.WRAP_CONTENT);
        set.connect(mMessageTextView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mMessageTextView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);
        set.connect(mMessageTextView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);

        //SearchTextView
        set.constrainWidth(mSearchTextView.getId(), ConstraintSet.MATCH_CONSTRAINT);
        set.constrainHeight(mSearchTextView.getId(), searchHeight);
        set.connect(mSearchTextView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mSearchTextView.getId(), ConstraintSet.LEFT, mAddressTextView.getId(), ConstraintSet.RIGHT);
        set.connect(mSearchTextView.getId(), ConstraintSet.RIGHT, mMessageTextView.getId(), ConstraintSet.LEFT);
        set.connect(mSearchTextView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);

        set.applyTo(this);
    }

    private void initTest() {
        if (isInEditMode()) {
            if (mAddressName == null) {
                mAddressName = "地址";
            }

            if (mSearchText == null) {
                mSearchText = "搜索";
            }

            if (mMessageTitle == null) {
                mMessageTitle = "消息";
            }

            if (mSearchResId <= 0) {
                mSearchResId = R.mipmap.icon_nav_search_black;
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (onNavgationBarClickListener == null) {
            return;
        }

        switch (v.getId()) {
            case R.id.iv_nav_bar_menu: //地址
                onNavgationBarClickListener.onAddress(v);
                break;
            case R.id.iv_nav_bar_search: //搜索
                onNavgationBarClickListener.onSearchClick(v);
                break;
            case R.id.iv_nav_bar_message: //消息
                onNavgationBarClickListener.onMessage(v);
                break;
            default:
                break;
        }
    }

    public void changeNavBarDrawable(boolean isWhite) {
        if (mDrawableIsWhiteTheme == isWhite) {
            return;
        }

        mDrawableIsWhiteTheme = isWhite;

        if (mDrawableIsWhiteTheme) {
            setNavBarDrawableWhite();
        } else {
            setNavBarDrawableBlack();
        }
    }

    private void setNavBarDrawableBlack() {
        if (mAddressDrawableBlack == null) {
            mAddressDrawableBlack = ContextCompat.getDrawable(mContent, R.mipmap.icon_nav_location_black);
        }

        if (mMessageDrawableBlack == null) {
            mMessageDrawableBlack = ContextCompat.getDrawable(mContent, R.mipmap.icon_nav_message_black);
        }

        mAddressTextView.setTextColor(mAddressTextColor == ColorUtils.getColorById(mContent, R.color.color_282829) ? mAddressTextColor : ColorUtils.getColorById(mContent, R.color.color_282829));
        mMessageTextView.setTextColor(mMessageTextColor == ColorUtils.getColorById(mContent, R.color.color_282829) ? mMessageTextColor : ColorUtils.getColorById(mContent, R.color.color_282829));

        mAddressTextView.setCompoundDrawablesWithIntrinsicBounds(null, mAddressDrawableBlack, null, null);
        mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null, mMessageDrawableBlack, null, null);
    }

    private void setNavBarDrawableWhite() {
        if (mAddressDrawableWhite == null) {
            mAddressDrawableWhite = ContextCompat.getDrawable(mContent, R.mipmap.icon_nav_location_white);
        }

        if (mMessageDrawableWhite == null) {
            mMessageDrawableWhite = ContextCompat.getDrawable(mContent, R.mipmap.icon_nav_message_white);
        }

        mAddressTextView.setTextColor(Color.WHITE);
        mMessageTextView.setTextColor(Color.WHITE);

        mAddressTextView.setCompoundDrawablesWithIntrinsicBounds(null, mAddressDrawableWhite, null, null);
        mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null, mMessageDrawableWhite, null, null);
    }

    //设置地址
    public void setNavBarAddressText(@StringRes int resId) {
        setNavBarAddressText(getContext().getString(resId));
    }

    //设置地址
    public void setNavBarAddressText(String text) {
        if (mAddressTextView != null) {
            mAddressTextView.setText(text);
        }
    }

    //设置搜索text
    public void setNavBarSearchText(@StringRes int resId) {
        setNavBarSearchText(getContext().getString(resId));
    }

    //设置搜索text
    public void setNavBarSearchText(String text) {
        if (mSearchTextView != null) {
            mSearchTextView.setText(text);
        }
    }

    //设置消息text
    public void setNavBarMessageText(@StringRes int resId) {
        setNavBarAddressText(getContext().getString(resId));
    }

    //设置消息text
    public void setNavBarMessageText(String text) {
        if (mMessageTextView != null) {
            mMessageTextView.setText(text);
        }
    }

    public void setOnNavgationBarClickListener(OnHomeNavigationBarClickListener onNavgationBarClickListener) {
        this.onNavgationBarClickListener = onNavgationBarClickListener;
    }
}
